package com.base.cn.platform.os.controller.web.user.center.binding;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.user.binding.UserMobileOrEmailBindingService;
import com.base.cn.platform.os.service.manage.user.sms.UserSendSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 用户的手机或邮箱号绑定Controller
 *
 * @author s.li
 * @create 2018-06-05-15:57
 */
@Controller
@RequestMapping("/web/uc/binding")
public class WebUserMobileOrEmailBindingController extends WebBaseController {

    @Autowired
    private UserMobileOrEmailBindingService userMobileOrEmailBindingService;
    @Autowired
    private UserSendSmsService userSendSmsService;

    /**
     * 发送短信接口
     * @param mobile 手机号
     * @param status 状态 3绑定，4更换
     * @return Map<String,Object>
     */
    @RequestMapping("/sendBindingMobileCode")
    @ResponseBody
    public Map<String,Object> sendBindingMobileCode(@RequestParam("mobile") String mobile,
                                                    @RequestParam("status") int status){
        if(status!=3 && status!=4){
            return ResultUtil.ERROR("数据错误，短信发送失败");
        }
        return userSendSmsService.sendMobileCode(mobile,status,null);
    }

    /**
     * 验证原绑定的手机号(如果没有绑定过的手机号，将用不到该接口)
     * @param mobile 手机号
     * @param mobileCode 手机验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/verificationOldMobile")
    @ResponseBody
    public Map<String,Object> verificationOldMobile(@RequestParam("mobile") String mobile,
                                                    @RequestParam("mobileCode") String mobileCode){
        return userMobileOrEmailBindingService.verificationOldMobile(mobile,mobileCode);
    }

    /**
     * 绑定手机号
     * @param request
     * @param mobile 新的手机号
     * @param mobileCode 新手机号的验证码
     * @param oldMobileCode 原手机号的验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/bindingUserMobile")
    @ResponseBody
    public Map<String,Object> bindingUserMobile(HttpServletRequest request,
                                                @RequestParam("mobile") String mobile,
                                                @RequestParam("mobileCode") String mobileCode,
                                                @RequestParam(value = "oldMobileCode",required = false) String oldMobileCode){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return userMobileOrEmailBindingService.bindingUserMobile(userId,mobile,mobileCode,oldMobileCode);
    }

    //=====================================邮箱号绑定，开始===========================================
    /**
     * 发送邮件接口
     * @param email 邮箱号
     * @param status 状态 3=绑定邮箱号（用于验证新绑定的邮箱号），4=改绑邮箱号（用于验证原绑定的邮箱号）
     * @return Map<String,Object>
     */
    @RequestMapping("/sendBindingEmailCode")
    @ResponseBody
    public Map<String,Object> sendBindingEmailCode(@RequestParam("email") String email,
                                                   @RequestParam("status") int status){
        if(status!=3 && status!=4){
            return ResultUtil.ERROR("数据错误，邮件发送失败");
        }
        return userSendSmsService.sendEmailCode(email,status);
    }

    /**
     * 验证原绑定的邮箱号(如果没有绑定过的邮箱号，将用不到该接口)
     * @param email 邮箱号
     * @param emailCode 邮箱验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/verificationOldEmail")
    @ResponseBody
    public Map<String,Object> verificationOldEmail(@RequestParam("email") String email,
                                                    @RequestParam("emailCode") String emailCode){
        return userMobileOrEmailBindingService.verificationOldEmail(email,emailCode);
    }

    /**
     * 绑定邮箱号
     * @param request
     * @param email 新的邮箱号
     * @param emailCode 新邮箱号的验证码
     * @param oldEmailCode 原邮箱号的验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/bindingUserEmail")
    @ResponseBody
    public Map<String,Object> bindingUserEmail(HttpServletRequest request,
                                                @RequestParam("email") String email,
                                                @RequestParam("emailCode") String emailCode,
                                                @RequestParam(value = "oldEmailCode",required = false) String oldEmailCode){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return userMobileOrEmailBindingService.bindingUserEmail(userId,email,emailCode,oldEmailCode);
    }
    //=====================================邮箱号绑定，结束===========================================
}
